package problem739;

import java.util.Stack;

//739.每日温度
//https://leetcode.cn/problems/daily-temperatures/?envType=study-plan-v2&envId=top-100-liked


class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int n = temperatures.length;
        Stack<Integer> stack = new Stack<>();
        int[] ret = new int[n];
        for(int i = 0; i<n; i++) {
            if(stack.isEmpty()) stack.push(i);
            else if(temperatures[i] > temperatures[stack.peek()]) {
                while(!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                    int t = stack.pop();
                    ret[t] = i - t;
                }
                stack.push(i);
            }else {
                stack.push(i);
            }
        }
        return ret;
    }
}